package com.ybc.chinese.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.pgyersdk.update.PgyUpdateManager;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;
import com.ybc.chinese.CustomShareListener;
import com.ybc.chinese.R;
import com.ybc.chinese.db.DBManager;
import com.ybc.chinese.fragment.CollegeFragment;
import com.ybc.chinese.fragment.DiscoveryFragment;
import com.ybc.chinese.fragment.HomeFragment;
import com.ybc.chinese.fragment.MoreFragment;
import com.ybc.chinese.fragment.SpecialFragment;
import com.ybc.chinese.utils.Contants;
import com.ybc.chinese.utils.DialogUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.Bmob;

public class MainActivity extends BaseActivity {

    @Bind(R.id.tv_home)
    TextView tvHome;
    @Bind(R.id.tv_college)
    TextView tvCollege;
    @Bind(R.id.tv_special)
    TextView tvSpecial;
    @Bind(R.id.tv_discovery)
    TextView tvDiscovery;
    @Bind(R.id.tv_more)
    TextView tvMore;

    private Context mCtx;
    private Fragment mFrom;
    private HomeFragment mHomeFragment;
    private CollegeFragment mCollegeFragment;
    private SpecialFragment mSpecialFragment;
    private DiscoveryFragment mDiscoveryFragment;
    private MoreFragment mMoreFragment;
    private UMShareListener mShareListener;
    private ShareAction mShareAction;
    private UMWeb web;
    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCtx = this;
        ButterKnife.bind(this);
        requestUpdate();
        //第一：默认初始化
        Bmob.initialize(this, "c65e24d21c7b7d731c9b421bf1964586");
        setHasFragment(true);
        initView();
    }

    private void initView() {
        mHomeFragment = HomeFragment.newInstance();
        mCollegeFragment = CollegeFragment.newInstance();
        mSpecialFragment = SpecialFragment.newInstance();
        mDiscoveryFragment = DiscoveryFragment.newInstance();
        mMoreFragment = MoreFragment.newInstance();
        updateStatus(tvHome);
        mFrom = mHomeFragment;
        getSupportFragmentManager().beginTransaction().add(R.id.main_content, mFrom).commit();
        mShareListener = new CustomShareListener(MainActivity.this);
        web = new UMWeb("http://a.app.qq.com/o/simple.jsp?pkgname=com.ybc.chinese");
        web.setTitle(getString(R.string.app_name));
        web.setThumb(new UMImage(mCtx, R.mipmap.ic_logo));
        web.setDescription("最专业的"+getString(R.string.app_name)+"学习软件");
        /*增加自定义按钮的分享面板*/
        mShareAction = new ShareAction(MainActivity.this).setDisplayList(
                SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
                /*SHARE_MEDIA.SINA, */SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE)
                .setShareboardclickCallback(new ShareBoardlistener() {
                    @Override
                    public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
                        new ShareAction(MainActivity.this).withMedia(web).withText("有了它，妈妈再也不用担心我的学习了")
                                .setPlatform(share_media)
                                .setCallback(mShareListener)
                                .share();
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        DBManager.getInstance(this).closeDB();
    }

    /**
     * 检查更新
     */
    private void requestUpdate() {
        PgyUpdateManager.register(this, Contants.PROVIDE_PATH);
    }

    @OnClick(R.id.tv_home)
    public void home() {
        updateStatus(tvHome);
        switchFragment(mFrom, mHomeFragment);
        mFrom = mHomeFragment;
    }

    @OnClick(R.id.tv_college)
    public void college() {
        updateStatus(tvCollege);
        switchFragment(mFrom, mCollegeFragment);
        mFrom = mCollegeFragment;
    }

    @OnClick(R.id.tv_discovery)
    public void discovery() {
        updateStatus(tvDiscovery);
        switchFragment(mFrom, mDiscoveryFragment);
        mFrom = mDiscoveryFragment;
    }

    @OnClick(R.id.tv_more)
    public void more() {
        updateStatus(tvMore);
        switchFragment(mFrom, mMoreFragment);
        mFrom = mMoreFragment;
    }

    @OnClick(R.id.tv_special)
    public void shop() {
        updateStatus(tvSpecial);
        switchFragment(mFrom, mSpecialFragment);
        mFrom = mSpecialFragment;
    }

    private void updateStatus(View view) {
        if (view == tvHome) {
            setStatus(tvHome, true, false);
            setStatus(tvCollege, false, true);
            setStatus(tvSpecial, false, true);
            setStatus(tvDiscovery, false, true);
            setStatus(tvMore, false, true);
        } else if (view == tvCollege) {
            setStatus(tvHome, false, true);
            setStatus(tvCollege, true, false);
            setStatus(tvSpecial, false, true);
            setStatus(tvDiscovery, false, true);
            setStatus(tvMore, false, true);
        } else if (view == tvSpecial) {
            setStatus(tvHome, false, true);
            setStatus(tvCollege, false, true);
            setStatus(tvSpecial, true, false);
            setStatus(tvDiscovery, false, true);
            setStatus(tvMore, false, true);
        } else if (view == tvDiscovery) {
            setStatus(tvHome, false, true);
            setStatus(tvCollege, false, true);
            setStatus(tvSpecial, false, true);
            setStatus(tvDiscovery, true, false);
            setStatus(tvMore, false, true);
        } else if (view == tvMore) {
            setStatus(tvHome, false, true);
            setStatus(tvCollege, false, true);
            setStatus(tvSpecial, false, true);
            setStatus(tvDiscovery, false, true);
            setStatus(tvMore, true, false);
        }
    }

    private void setStatus(View view, boolean isSelected, boolean isClickable) {
        view.setSelected(isSelected);
        view.setClickable(isClickable);
    }

    /**
     * 切换页面的重载，优化了fragment的切换
     */
    public void switchFragment(Fragment from, Fragment to) {
        if (from == null || to == null)
            return;
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        if (!to.isAdded()) {
            // 隐藏当前的fragment，add下一个到Activity中
            transaction.hide(from).add(R.id.main_content, to).commitAllowingStateLoss();
        } else {
            // 隐藏当前的fragment，显示下一个
            transaction.hide(from).show(to).commitAllowingStateLoss();
        }
    }

    @Override
    public void onBackPressed() {
        dialog = DialogUtil.getCustomerDialog(mCtx, getString(R.string.exit_message), getString(R.string.share), getString(R.string.exit), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                mShareAction.open();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                finish();
            }
        });
        dialog.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(MainActivity.this).onActivityResult(requestCode, resultCode, data);

    }
}
